<?php

namespace App\Admin\Controllers;

use App\Models\Teacher;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;


class TeacherDetailController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        $user_id = Admin::user()->id;

        $teacher = Teacher::where('user_id',$user_id)->first();
        $teacher_id = $teacher->id;
        $route = 'admin/tm/profile/' . $teacher_id;
        return redirect($route);
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('教师个人信息')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
        ->header('教学管理')
        ->description('编辑个人信息')
        ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('新建教师信息')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Teacher);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Teacher::findOrFail($id));

        $show->picture('照片')->image('','150','150');
        $show->teacher_id('教工号');
        $show->name('教师姓名');
        $show->birthday('出生日期');
        $show->sex('性别')->as(function ($sex) {
            $sexStr = '';
            switch ($sex) {
                case '0':
                    $sexStr = '男';
                    break;
                case '1':
                    $sexStr = '女';
                    break;
                case '2':
                    $sexStr = '其他';
                    break;
            }
            return $sexStr;
        });
        $show->nation('民族');
        $show->degree('学位');
        $show->id_card('身份证');
        $show->entry_date('入职日期');
        $show->mobile('手机号');
        $show->qq('QQ');
        $show->email('邮箱');
        $show->politics_status('政治面貌')->as(function ($politicsStatus) {
            $maritalStatusArr = [
                '01' => '中共党员',
                '02' => '中共预备党员',
                '03' => '共青团员',
                '04' => '民革党员',
                '05' => '民盟盟员',
                '06' => '民建会员',
                '07' => '民进会员',
                '08' => '农工党党员',
                '09' => '致公党党员',
                '10' => '九三学社社员',
                '11' => '台盟盟员',
                '12' => '无党派人士',
                '13' => '群众'
            ];
            $politicsStatus = $maritalStatusArr[$politicsStatus];

            return $politicsStatus;
        });
        $show->teacher_certification('是否有教师资格证')->as(function ($certification) {
            return $certification ? $certification = '具有教师资格证': $certification = '无教师资格证';
        });
        $show->marital_status('婚姻状况')->as(function ($maritalStatus) {
            $maritalStatusArr = [
                '0' => '未婚',
                '1' => '已婚',
                '2' => '离婚',
                '3' => '丧偶',
            ];

            $maritalStatus = $maritalStatusArr[$maritalStatus];
            return $maritalStatus;
        });
        $show->on_duty('是否离职')->as(function ($onDuty) {
            return $onDuty ? $onDuty = '离职': $onDuty = '在岗';
        });
        $show->teaching_motto('教育格言')->unescape();
        $show->created_at('创建日期');
        $show->updated_at('修改时间');

        $show->panel()->tools(function ($tools) {
//                $tools->disableEdit();
                $tools->disableList();
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Teacher());

        $form->text('teacher_id', '教工号')->setWidth(2, 2)->rules('required', [
            'required' => '教工号必填'
        ]);
        $form->text('name', '教师姓名')->setWidth(2, 2)->rules('required', [
            'required' => '教师姓名必填'
        ]);
        $form->date('birthday', '出生日期')->default(date('Y-m-d'));
        $form->radio('sex','性别')->options([0 => ' 男', 1 => ' 女', '2' => ' 其他']);
        $form->select('nation','民族')->options(['汉族' => '汉族', '壮族' => '壮族', '回族' => '回族', '满族' => '满族', '维吾尔族' => '维吾尔族', '苗族' => '苗族', '彝族' => '彝族', '土家族' => '土家族', '藏族' => '藏族', '蒙古族' => '蒙古族', '侗族' => '侗族', '布依族' => '布依族', '瑶族' => '瑶族', '白族' => '白族', '朝鲜族' => '朝鲜族', '哈尼族' => '哈尼族', '黎族' => '黎族', '哈萨克族' => '哈萨克族', '傣族' => '傣族', '畲族' => '畲族', '傈僳族' => '傈僳族', '东乡族' => '东乡族', '仡佬族' => '仡佬族', '拉祜族' => '拉祜族', '佤族' => '佤族', '水族' => '水族', '纳西族' => '纳西族', '羌族' => '羌族', '土族' => '土族', '仫佬族' => '仫佬族', '锡伯族' => '锡伯族', '柯尔克孜族' => '柯尔克孜族', '景颇族' => '景颇族', '达斡尔族' => '达斡尔族', '撒拉族' => '撒拉族', '布朗族' => '布朗族', '毛南族' => '毛南族', '塔吉克族' => '塔吉克族', '普米族' => '普米族', '阿昌族' => '阿昌族', '怒族' => '怒族', '鄂温克族' => '鄂温克族', '京族' => '京族', '基诺族' => '基诺族', '德昂族' => '德昂族', '保安族' => '保安族', '俄罗斯族' => '俄罗斯族', '裕固族' => '裕固族', '乌孜别克族' => '乌孜别克族', '门巴族' => '门巴族', '鄂伦春族' => '鄂伦春族', '独龙族' => '独龙族', '赫哲族' => '赫哲族', '高山族' => '高山族', '珞巴族' => '珞巴族', '塔塔尔族' => '塔塔尔族'])->setWidth(2, 2);
        $form->select('degree', '学位')->options(['学士' => '学士', '硕士' => '硕士', '博士' => '博士', '无' => '无'])->setWidth(2, 2);
        $form->text('id_card', '身份证')->setWidth(2, 2)->rules('required', [
            'required' => '教师身份证必填'
        ]);
        $form->date('entry_date', '入职日期')->default(date('Y-m-d'));
        $form->mobile('mobile', '手机号');
        $form->text('qq', 'QQ')->setWidth(2, 2);
        $form->email('email', '邮箱')->setWidth(2, 2);
        $form->select('politics_status', '政治面貌')->options(['01' => '中共党员', '02' => '中共预备党员', '03' => '共青团员', '04' => '民革党员', '05' => '民盟盟员', '06' => '民建会员', '07' => '民进会员', '08' => '农工党党员', '09' => '致公党党员', '10' => '九三学社社员', '11' => '台盟盟员', '12' => '无党派人士', '13' => '群众'])->setWidth(2, 2);
        $form->switch('teacher_certification', '是否有教师资格证')->states([ 'on'  => ['value' => 1, 'text' => '有'], 'off' => ['value' => 0, 'text' => '无'],]);
        $form->select('marital_status', '婚姻状况')->options([0 => '未婚', 1 => '已婚', 2 => '离婚', 3 => '丧偶'])->setWidth(1, 2);
        $form->image('picture', '照片')->uniqueName()->setWidth(3, 2);
        $form->hidden('user_id');
        $form->switch('on_duty', '是否离职')->states([ 'on'  => ['value' => 1, 'text' => '离职'], 'off' => ['value' => 0, 'text' => '在岗'],]);
        $form->editor('teaching_motto', '教育格言');


        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
            $tools->disableDelete();
//            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        $form->saving(function ($model) {
            $id = $model->model()->id;
            if(!$id){
                $request = request()->all();
                $teacherId = $request['teacher_id'];
                $cardId = $request['id_card'];
                $teacherResult = Teacher::where('teacher_id', $teacherId)->first();
                $cardResult = Teacher::where('id_card', $cardId)->first();
                if(!empty($teacherResult) or !empty($cardResult)){
                    $title = '提示';
                    $message = '教工号或身份证号已存在';
                }

                if(!empty($title)){
                    $error = new MessageBag([
                        'title'   => $title,
                        'message' => $message,
                    ]);
                    return back()->with(compact('error'))->withInput();
                }
            }

            $username = $model->teacher_id;
            $name = $model->name;
            $user_id = $model->user_id;
            $admin_user = DB::table('admin_users')->where('id', $user_id)->first();
            $datetime = date('Y-m-d H:i:s');
            if(!empty($admin_user)) {
                DB::table('admin_users')->where('id', $user_id)->update([
                    'username' => "$username"
                    ,'name' => "$name"
                    ,'updated_at' => "$datetime"
                ]);
            }else {
                $password = bcrypt('123456');
                $userId = DB::table('admin_users')->insertGetId([
                    'username' => "$username"
                    ,'password' => "$password"
                    ,'name' => "$name"
                    ,'created_at' => "$datetime"
                    ,'updated_at' => "$datetime"
                ]);
                DB::table('admin_role_users')->insert([
                    'role_id' => 2
                    ,'user_id' => $userId
                ]);
                $model->user_id = $userId;
            }
        });
        return $form;
    }
}
